﻿#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using AzureMRCore.DataTypes;
using AzureMRCore.Primitives;

namespace AzureMRCore.InputFormat
{
    public class DoubleMatrixFormat : IInputFormat<IntKey, MatrixValue<double>>
    {
        private int _counter;
        private Boolean _isKeyed;
        private double[][] _values;
        private int _numVectors, _vectorLength, _row, _rowOffset;
        private long _size;
        private bool _isRead;

        #region IInputFormat<IntKey,double[]> Members

        public void Init(string inputFile)
        {
            BinaryReader _reader = new BinaryReader(File.Open(inputFile, FileMode.Open));
            FileInfo file = new FileInfo(inputFile);
            _size = file.Length;
            _numVectors = _reader.ReadInt32();
            _vectorLength = _reader.ReadInt32();
            _row = _reader.ReadInt32();
            _rowOffset = _reader.ReadInt32();

            _values = new double[_numVectors][];

            _isKeyed = _reader.ReadBoolean();
            ReadAll(_reader);
            _reader.Close();
        }

        public void Init(IDictionary<int, BCastDataProduct> inputRecords)
        {
            throw new NotImplementedException();
        }

        public List<KeyValuePair<IntKey, MatrixValue<double>>> GetAllPairs()
        {
            MatrixValue<double> matrixValue = MatrixValue<double>.GetInstance(_values, _numVectors,
                                                                              _vectorLength, _row, _rowOffset);
            var list = new List<KeyValuePair<IntKey, MatrixValue<double>>>
                           {
                               new KeyValuePair<IntKey, MatrixValue<double>>(IntKey.GetInstance(0),
                                                                             matrixValue)
                           };
            return list;
        }

        public IntKey GetCurrentKey()
        {
            IntKey intKey = new IntKey {Key = 0};
            return intKey;
        }

        public MatrixValue<double> GetCurrentValue()
        {
            return MatrixValue<double>.GetInstance(_values, _numVectors, _vectorLength, _row, _rowOffset);
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Next()
        {
            if (!_isRead)
            {
                _isRead = true;
                return true;
            }

            return false;
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool HasNext()
        {
            return !(_isRead);
        }

        public void Close()
        {
        }

        #endregion

        private double[][] ReadAll(BinaryReader reader)
        {
            while (_counter < _numVectors)
            {
                int key = reader.ReadInt32();
                double[] vector = new double[_vectorLength + 1];
                for (var i = 0; i < _vectorLength; i++)
                {
                    vector[i] = reader.ReadDouble();
                }
                vector[_vectorLength] = key;
                _values[_counter] = vector;
                _counter++;
            }
            return _values;
        }
    }
}